﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.BusinessLogic.Common
{
    /// <summary>
    /// Class used as a (guid based enumeration) list that holds the possible types for Language.
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.LanguageFactory, MarketPulse.DataAccess.Factory", "FetchInfoList")]
    public class LanguageInfoList : MarketPulseReadOnlyListBase<LanguageInfoList, LanguageInfo>
    {
        #region Member Variables

            /// <summary>List to hold all the possible Language values</summary>
            private static LanguageInfoList mLanguages;

        #endregion

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// via only the static factory methods provided.
            /// </summary>
            private LanguageInfoList()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method used to retrieve a list of Language objects. Internally this accesses a static field so this
                /// class is effectively a lazily instantiated singleton.
                /// </summary>
                public static LanguageInfoList GetList()
                {
                    // Load the list if it's null
                    if (mLanguages == null)
                    {
                        mLanguages = DataPortal.Fetch<LanguageInfoList>();
                    }
                    return mLanguages;
                }

            #endregion

            /// <summary>
            /// Method used to find a single LanguageInfo object from the collection based on the language code supplied
            /// </summary>
            /// <param name="languageCode">The code that uniquely identifies the Language Info object to find</param>
            /// <returns>A populated Language Info object</returns>
            public LanguageInfo FindByCode(string languageCode)
            {
                var result = (from language in this.Items
                              where language.LanguageISOCode2 == languageCode
                              select language).Single();

                return (LanguageInfo)result;
            }

            /// <summary>
            /// Method used to find a single LanguageInfo object from the collection based on the unique identifier supplied
            /// </summary>
            /// <param name="languageGuid">The guid that uniquely identifies the Language Info object to find</param>
            /// <returns>A populated Language Info object</returns>
            public LanguageInfo FindByGuid(Guid languageGuid)
            {
                var result = (from language in this.Items
                              where language.GUID == languageGuid
                              select language).Single();

                return (LanguageInfo)result;
            }

        #endregion

    }
}
